package b2.widget.tank
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.scene.*;
	import b2.util.*;
	import b2.widget.*;
	import b2.widget.tab.*;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.filters.*;
	import flash.geom.Point;
	import flash.text.*;
	import flash.ui.Mouse;
	
	public class TankSlot extends Sprite
	{
		public var tankId:int;
		private var tankTitle:TextField;
		private var fightPoint:TextField;
		private var tankIcon:Sprite;
		private var tankContainer:Sprite;
		
		public function TankSlot()
		{
			super();
			//背景框
			var SlotBgr:Class = ResourceHelper.getInstance().getClassByName("TankSlotBgr");
			var slotBgr:Sprite = new SlotBgr();
			addChild(slotBgr);
			//坦克容器
			tankContainer = new Sprite();
			tankContainer.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			tankContainer.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			addChild(tankContainer);
			//坦克名字
			tankTitle = new TextField();
			tankTitle.selectable = false;
        	tankTitle.autoSize = TextFieldAutoSize.LEFT;
			tankTitle.defaultTextFormat = new TextFormat("微软雅黑", 12, 0xFFFF00);
			tankTitle.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			tankTitle.x = 15;
			tankTitle.y = 8;
			addChild(tankTitle);
			//战力图标
			var CampIcon:Class = ResourceHelper.getInstance().getClassByName("CampIcon");
			var fpIcon:Sprite = new CampIcon();
			fpIcon.width = 20;
			fpIcon.height = 20;
			fpIcon.x = 10;
			fpIcon.y = 50;
			addChild(fpIcon);
			//坦克战力
			fightPoint = new TextField();
			fightPoint.selectable = false;
        	fightPoint.autoSize = TextFieldAutoSize.LEFT;
			fightPoint.defaultTextFormat = new TextFormat("微软雅黑", 12, 0xFFFFFF);
			fightPoint.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			fightPoint.x = fpIcon.x + 25;
			fightPoint.y = 50;
			addChild(fightPoint);
		}
		
		public function loadTank(tankId:int):void
		{
			//移除原有元素
			while (tankContainer.numChildren > 0)
				tankContainer.removeChildAt(0);
			this.tankId = tankId;
			var tankInfo:TankInfo = PlayerInfoManager.getInstance().tanks[tankId];
			var tankConfig:TankConfig = Config.objectConfigs[tankInfo.index];
			//坦克名字
			var basicLevelName:String = Config.basicConfig.tankBasicLevel[tankInfo.basicLevel];
			var strengthenLevelName:String = Config.basicConfig.tankStrengthenLevel[tankInfo.strengthenLevel];
			tankTitle.text = tankConfig.name + " · " + basicLevelName + strengthenLevelName;
			fightPoint.text = tankInfo.fightPoint.toString();
			//背景框
			var ObjectItemBgr:Class = ResourceHelper.getInstance().getClassByName("ObjectItemBgr" + tankInfo.basicLevel);
			var objectItemBgr:Sprite = new ObjectItemBgr();
			objectItemBgr.x = -10;
			objectItemBgr.y = 30;
			objectItemBgr.width = 186;
			objectItemBgr.height = 190;
			tankContainer.addChild(objectItemBgr);
			//背景框遮罩
			var rrect:Sprite = new Sprite();
			rrect.graphics.beginFill(0x000000);
			rrect.graphics.drawRoundRect(0, 0, 152, 162, 30, 30);
			rrect.graphics.endFill();
			addChild(rrect);
			rrect.x = 5;
			rrect.y = 44;
			tankContainer.addChild(rrect);
			objectItemBgr.mask = rrect;
			//图标
			if (tankIcon != null && tankIcon.parent != null)
				tankIcon.parent.removeChild(tankIcon);
			var TankIcon:Class = ResourceHelper.getInstance().getClassByName(tankConfig.iconImage);
			tankIcon = new TankIcon();
			tankIcon.x = 20;
			tankIcon.y = 50;
			//齐天大圣
			if (tankInfo.index == 10015)
			{
				tankIcon.width = 165;
				tankIcon.height = 185;
			}
			//死亡骑士
			else if (tankInfo.index == 10020)
			{
				tankIcon.width = 144;
				tankIcon.height = 144;
			}
			else
			{
				tankIcon.width = 120;
				tankIcon.height = 120;
			}
			tankContainer.addChild(tankIcon);
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
			tankContainer.filters = [new ColorMatrixFilter(BFont.brightFilter)];
			var globalPoint:Point = this.parent.localToGlobal(new Point(this.x, this.y));
			var tankMessage:TankMessage = new TankMessage(tankId); 
			tankMessage.x = globalPoint.x + 164;
			tankMessage.y = globalPoint.y;
			if (tankMessage.x + tankMessage.width > BFont.stageWidth)
				tankMessage.x = globalPoint.x - tankMessage.width;
			if (tankMessage.y + tankMessage.height > BFont.stageHeight)
				tankMessage.y = BFont.stageHeight - tankMessage.height;
			WidgetManager.getInstance().popOutMouseOverMessage(tankMessage);
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
			tankContainer.filters = null;
			WidgetManager.getInstance().closeMouseOverMessage();
		}
	}
}